package com.winit.psc.web;

import com.winit.common.base.BaseController;
import com.winit.psc.manager.PscManager;
import com.winit.psc.model.MatrixPsc;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by wangjian on 2017/10/27.
 */
@RestController
@Api(value = "psc", tags = {"psc"}, description = "psc")
@RequestMapping(value = "/api/v1/psc")
public class PscController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(PscController.class);

    @Autowired
    private PscManager manager;

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取psc列表")
    public
    @ResponseBody
    Map getPsc(@ApiParam(value = "psc id", required = false) @RequestParam(value = "id", required = false) String id,
                @ApiParam(value = "psc code，可以逗号分隔的string组成", required = false) @RequestParam(value = "code", required = false) String code,
                @ApiParam(value = "psc type为P 或者 S", required = false) @RequestParam(value = "type", required = false) String type,
                @ApiParam(value = "psc name", required = false) @RequestParam(value = "name", required = false) String name ) {
        if ((!"P".equals(type)) && (!"S".equals(type)) && (null != type)){
            return this.getRtn(BaseController.REQUEST_ERROR, "type只能是S或者P");
        }
        List<String> codes = StringUtils.isEmpty(code) ? null : Arrays.asList(code.split(","));
        List<MatrixPsc> list = this.manager.getList(id, type, codes, name);
        return this.getRtn(BaseController.REQUEST_SUCCESS, "success", list);
    }

    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取psc列表")
    public
    @ResponseBody
    Map tree(@ApiParam(value = "psc id", required = false) @RequestParam(value = "id", required = false) String id,
             @ApiParam(value = "psc code", required = false) @RequestParam(value = "code", required = false) String code,
             @ApiParam(value = "psc type为P 或者 S", required = false) @RequestParam(value = "type", required = false) String type,
             @ApiParam(value = "psc parent_id", required = false) @RequestParam(value = "parentId", required = false) String parentId,
             @ApiParam(value = "psc name", required = false) @RequestParam(value = "name", required = false) String name,
             @ApiParam(value = "withChildren, 是否带回子节点，0-否，1-是", required = false) @RequestParam(value = "withChildren", required = false) Integer withChildren) {
        if ((!"P".equals(type)) && (!"S".equals(type)) && (null != type)){
            return this.getRtn(BaseController.REQUEST_ERROR, "type只能是S或者P");
        }
        if (null == withChildren){
            withChildren = 0;
        }
//        List<String> codes = StringUtils.isEmpty(code) ? null : Arrays.asList(code.split(","));
        List<MatrixPsc> list = this.manager.tree(id, type, code, name, parentId, withChildren);
        return this.getRtn(BaseController.REQUEST_SUCCESS, "success", list);
    }

}
